//
//  ViewController.m
//  折线图
//
//  Created by rimi on 16/3/15.
//  Copyright © 2016年 Zhangzhen. All rights reserved.
//

#import "ChartViewController.h"
#import "ZZLineChart.h"
#import "ZZLineChartPoint.h"
#import "FitOfScreen.h"
#import "WeatherDataRequest.h"
@interface ChartViewController ()
@property (nonatomic, strong) ZZLineChart *lineCharView;
@property (nonatomic, strong) UIButton *drawLineChartButton;
@property (nonatomic, strong)  NSMutableArray *array2;
@property (nonatomic, strong) NSArray *temperatureLineDataarray;
@property (nonatomic, strong) NSMutableArray *juHeArray;
@end

@implementation ChartViewController
- (instancetype)init:(NSString *)city
{
    self = [super init];
    if (self) {
        [self InitDataSource:city];
    }
    return self;
}

- (NSMutableArray *)juHeArray{
    if (!_juHeArray) {
        _juHeArray = [[NSMutableArray alloc]init];
        
    }
    return _juHeArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    self.lineCharView = [[ZZLineChart alloc] initWithFrame:CGRectMake(0, LRLCurrentScreenHeight / 3, CGRectGetWidth(self.view.bounds), LRLCurrentScreenHeight / 3 )];
    [self.view addSubview:self.lineCharView];
    self.drawLineChartButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.drawLineChartButton.frame = CGRectMake(180, 375, 50, 44);
    [self.drawLineChartButton addTarget:self action:@selector(drawLineChart) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.drawLineChartButton];
}

- (void)InitDataSource:(NSString *)city {
    WeatherDataRequest *TemperatureLineData = [[WeatherDataRequest alloc]init];
    [TemperatureLineData getData:^{
        if ([TemperatureLineData.error isEqualToString:@"1"]) {
        
        }else{
        //给折线图赋值
        self.juHeArray = TemperatureLineData.juHeDataSource;
        self.lineCharView.temperature = [self AddTemperatureSymbolWithArray:self.juHeArray];
        self.lineCharView.pointArray = [self.lineCharView arrayLinePointArray:[self arrayWithLineChartArray:[self TemperaturelineDataWith:self.juHeArray]]];
        self.lineCharView.DetailsWeather = [self TemperatureDataForDetailsWith:self.juHeArray];
        //刷新数据
        [self.lineCharView reloadData];
        }
        
    } withcity:city];

}
//折线图温度高度

- (NSMutableArray *)arrayWithLineChartArray:(NSArray *)array{
    NSInteger minNum = [self getMin:array];
    NSInteger maxNum = [self getMax:array];
    double temp = maxNum - minNum;
    NSMutableArray *array2 = [[NSMutableArray alloc]init];
    for (int i = 0; i < 5; i++) {
        double num1 = [array[i] integerValue]-minNum;
        double height = (num1/temp)*4+0.7;
//        NSLog(@"温度%d的高度：%lf",i,height);
        NSString *string = [[NSString alloc]initWithFormat:@"%l.1f",height ];
        [array2 addObject:string];
    }
    return array2;
}




//获取最大值
- (NSInteger)getMax:(NSArray *)array{
    NSInteger max = [array[0] integerValue];
    for (int i = 0; i < 5; i++) {
        if(max < [array[i] integerValue]){
            max = [array[i] integerValue];
        }
    }
    return max;
    
}


//获取最小值
- (NSInteger)getMin:(NSArray *)array{
    NSInteger min = [array[0] integerValue];
    for (int i = 0; i < 5; i++) {
        if(min > [array[i] integerValue]){
            min = [array[i] integerValue];
        }
    }
    return min;
    
}

- (void)drawLineChart {
    
    [self.lineCharView startDrawlineChart];
}

- (NSMutableArray *)TemperaturelineDataWith:(NSMutableArray *)array{
    NSMutableArray *array1 = [[NSMutableArray alloc]init];
    for (int i = 0 ; i < 5; i ++) {
        if (![array[i][@"info"] isKindOfClass:[NSNull class]]){
            NSString *string =  array[i][@"info"][@"day"][2];
            [array1 addObject:string];
        }else{
            NSString *string = @"0";
            [array1 addObject:string];
        }
        
    }
    return array1;
    
}
- (NSMutableArray *)AddTemperatureSymbolWithArray:(NSMutableArray *)array{
    NSMutableArray *array1 = [[NSMutableArray alloc]init];
    for (int i = 0 ; i < 5; i ++) {
        if (![array[i][@"info"] isKindOfClass:[NSNull class]]) {
            NSMutableString *string =  array[i][@"info"][@"day"][2];
            NSString *string2 = @"°";
            string = [string stringByAppendingString:string2];
            [array1 addObject:string];
        }else{
            NSString *string = @"无信息";
            [array1 addObject:string];
        }
        
        
        
        
    }
    
    return array1;
    
}

- (NSMutableArray *)TemperatureDataForDetailsWith:(NSMutableArray *)array{
    NSMutableArray *array1 = [[NSMutableArray alloc]init];
    for (int i = 0; i < 5; i ++) {
        if (![array[i][@"info"] isKindOfClass:[NSNull class]]){
            NSString *string = array[i][@"info"][@"day"][1];
            [array1 addObject:string];
        }else{
            NSString *string = @"无";
            [array1 addObject:string];
        }
        
    }
    return array1;
}
@end
